package com.gxuwz.icefrede.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.icefrede.entity.Product;
import com.gxuwz.icefrede.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

//    @Value("${upload.dir}")  // 在application.properties中定义这个值
//    private static String UPLOAD_DIR;
//    @RequestMapping("/upload")
//    public ResponseEntity<String> uploadFile(MultipartFile file) {
//        if (file.isEmpty()) {
//            return ResponseEntity.badRequest().body("文件为空");
//        }
//        try {
//            byte[] bytes = file.getBytes();
//            Path path = Paths.get(UPLOAD_DIR, file.getOriginalFilename());
//            Files.write(path, bytes);
//            String relativePath = "/upload/product/" + file.getOriginalFilename();  // 假设/upload/product/是Web可访问的
//            return ResponseEntity.ok(relativePath);
//        } catch (IOException e) {
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败: " + e.getMessage());
//        }
//    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    //pageNum: 当前页码。
    //pageSize: 每页显示的记录数
    @RequestMapping("/pro_list")

    public Page<Product> productList(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        //新建分页构造函数Page<T> T为目标实体类
        Page<Product> page = new Page<Product>(pageNum, pageSize);
        //result为分页查询结果
        Page<Product> result = productService.page(page);
        return result;
    }

    /**
     * 添加一条记录
     * @param product
     * @return
     */
    @RequestMapping("/pro_add")
    public String productSave(@RequestBody Product product){
        if (product == null){
            return "产品信息不能为空";
        }else {
            boolean b = productService.save(product);
            if (b){
                return "添加成功";
            }
            return "添加失败";
        }
    }

    /**
     * 批量添加记录
     * @param productCollection
     * @return
     */
    @RequestMapping("/pro_addBatch")
    public String productSaveBatch(Collection<Product> productCollection){
        boolean b = productService.saveBatch(productCollection);
        if (b){
            return "backend/pages/product/pro_list.html";
        }
        return "backend/pages/product/pro_addBatch.html";
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @RequestMapping("/pro_delete")
    public String productDeleteById(@RequestParam Integer id){
        boolean b = productService.removeById(id);
        if (b){
            return "删除成功";
        }
        return null;
    }

//    public String productDeleteByIds(Collection<>){
//        boolean b = productService.removeByIds();
//    }

    /**
     * 根据ID修改
     * @param product
     * @return
     */
    @RequestMapping("/pro_update")
    public String productUpdate(@RequestBody Product product){
        boolean b = productService.updateById(product);
        if (b){
            return product.getName();
        }
        return null;
    }

    /**
     * 修改库存
     * @param id
     * @param stock
     * @return
     */
    @RequestMapping("/pro_update_num")
    public String productUpdateNum(@RequestParam Integer id, @RequestParam Integer stock,@RequestParam Double price){
        Product product = productService.getById(id);
        System.out.println(product);
        product.setPrice(price);
        product.setStock(stock);
        boolean b = productService.updateById(product);
        if (b){
            return "修改库存成功";
        }
        return null;
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @RequestMapping("/pro_query")
    public Product productQueryById(@RequestParam Integer id){
        return productService.getById(id);
    }

    /**
     * 根据名称模糊分页查询
     * @param name
     * @return
     */
    @RequestMapping("/searchByName")
    public Page<Product> searchProductByName(@RequestParam String name, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        Page<Product> page = new Page<Product>(pageNum, pageSize);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>();
        wrapper.like(Product::getName, name);
        return productService.page(page, wrapper);
    }
}
